<template>
  <div class="logincontain">
    <el-card>
      <el-form :model="form" ref="form" :rules="rules" :inline="true">
        <h3 class="logintitle">系统登陆</h3>
        <el-form-item label="姓名" prop="username">
          <el-input type="text" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="btnlogin">
          <el-button type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getMenu } from '@/api/index.js'
import { mapState, mapMutations } from 'vuex'
// import Mock from 'mockjs'
import Cookie from 'js-cookie'
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapState({
      menu: state => state.tab.menu
    })
  },
  methods: {
    ...mapMutations(['setmenu', 'addmenu']),
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // const token = Mock.Random.guid()
          // Cookie.set('token', token)
          // this.$router.replace('/')
          getMenu(this.form).then(data => {
            console.log(data)
            if (data.data.code == 20000) {
              Cookie.set('token', data.data.token)
              this.$router.replace('/')
              this.$message({
                message: data.data.data.message,
                type: 'success'
              })
              this.setmenu(data.data.data.menu)
              this.addmenu(this.$router)
            } else {
              this.$message.error(data.data.data.message)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.logincontain {
  margin: 180px auto;
  width: 350px;
  // padding: 35px 35px 15px 35px;
  border-radius: 15px;
  border: 1px solid #eaeaea;
  .logintitle {
    text-align: center;
    margin-bottom: 40px;
    font-weight: normal;
  }
  .btnlogin {
    display: flex;
    justify-content: center;
  }
}
</style>
